﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace homework4
{
    public partial class Form1 : Form
    {
        String path1=null;
        String path2=null;
        String destination= Application.StartupPath + "\\data\\result.txt";
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (!Directory.Exists(Application.StartupPath + "\\data"))
                Directory.CreateDirectory(Application.StartupPath + "\\data");  
                this.button2.Enabled = false;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "请选择第一个文本";
            openFileDialog.Filter = "文本文件|*.txt";
            openFileDialog.FilterIndex = 1;
            openFileDialog.Multiselect = false;
            if (openFileDialog.ShowDialog() == DialogResult.OK) { 
                path1 = openFileDialog.FileName;
            }
            openFileDialog.Dispose();
            if (path1 != null && path2 != null)
            {
                this.button2.Enabled = true;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "请选择第二 个文本";
            openFileDialog.Filter = "文本文件|*.txt";
            openFileDialog.FilterIndex = 1;
            openFileDialog.Multiselect = false;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                path2 = openFileDialog.FileName;
            }
            openFileDialog.Dispose();
            if (path1 != null && path2 != null)
            {
                this.button2.Enabled = true;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (!File.Exists(destination))
            {
                using(File.CreateText(destination)){ }
            }
            foreach (String line in File.ReadLines(path1, Encoding.UTF8))
            {
                File.AppendAllText(destination, line+"\n", Encoding.UTF8);
            }
            foreach (String line in File.ReadLines(path2, Encoding.UTF8))
            {
                File.AppendAllText(destination, line + "\n", Encoding.UTF8);
            }
        }

        
    }
}
